package cn.itsource.ymcc.mq;

import cn.itsource.ymcc.constants.BaseConstants;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.dto.AlipayNotifyDto;
import cn.itsource.ymcc.service.ICourseOrderService;
import cn.itsource.ymcc.to.PlaceCourseOrderTo;
import com.alibaba.fastjson.JSON;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@RocketMQMessageListener(topic = BaseConstants.OrderMQ.TOPIC_COURSE_ORDER_DEALY,
        selectorExpression=BaseConstants.OrderMQ.TAGS_COURSE_ORDER_DEALY
        ,consumerGroup = "service-order-outtime-consumer"
        ,messageModel = MessageModel.BROADCASTING
    )
public class PayOutTimeConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private ICourseOrderService courseOrderService;

    @Override
    public void onMessage(MessageExt message) {
        String msg = new String(message.getBody(), CharsetUtil.UTF_8);
        log.info("支付超时消费者, {} ",msg);
        PlaceCourseOrderTo placeCourseOrderTo = JSON.parseObject(msg, PlaceCourseOrderTo.class);
        //支付超时关单
        courseOrderService.closeOrder(placeCourseOrderTo);
        //如果是秒杀，还要考虑退出
    }
}
